<!DOCTYPE HTML>
<html>
<head>
	<title>广告管理</title>
	{{template "inc/mate.html" .}}
    {{template "inc/easyui_css.html" .}}
    {{template "inc/ie_supper.html" .}}
</head>
<body class="easyui-layout">
	<div data-options="region:'north'" style="height:50px;padding: 10px;">
		{{if authas "/man/advert/toadd" .PhyAuths}}
			<a href="{{.baseUrl}}/man/advert/toadd" class="easyui-linkbutton" style="border-color: #4cae4c; background-image: linear-gradient(rgb(76,174,76) 0px, rgb(68,157,68) 100%);">添加广告</a>
		{{end}}
	</div>
	<div data-options="region:'center'">
		<table id="dg" class="easyui-datagrid" style="width:100%;height:100%" data-options="url:'{{.baseUrl}}/man/advert/findAdverts',singleSelect:true,collapsible:true,method:'post',pagination:true,pageList:[15,20,25,30],pageSize:15">
	     	<thead>
	            <tr>
	            	<th data-options="field:'aa',align:'left',formatter:fmtAction">操作</th>
	                <th data-options="field:'adStatus',align:'left',formatter:fmtStatus">状态</th>
	                <th data-options="field:'adTitle',align:'left'">广告标题</th>
	                <th data-options="field:'adCSPrice',align:'left'">点击价格(元)</th>
	                <th data-options="field:'adTCount',align:'center'">推广次数</th>
	                <th data-options="field:'adCount',align:'left',formatter:fmtCount">点击量</th>
	                <th data-options="field:'math',align:'left',formatter:fmtMath">总代理分成(元)</th>
	                <th data-options="field:'adUrl',align:'left'">广告地址</th>
	            </tr>
	        </thead>
	    </table>
	</div>
	<div id="dlg" class="easyui-dialog" data-options="closable: true,title:'添加浏览量',closed:true,draggable:false,resizable:false,modal:true" style="width:360px;padding:20px 20px; height:115px; ">
		<input type="hidden" id="ad_click_id" value="" />
		<input class="easyui-numberspinner" id="adCount" name="adTCount" data-options="label:'浏览次数',increment:1,min:1"  style="width:180px;">
		<a href="javascript:void(0)" class="easyui-linkbutton" id="btn_submit" style="width:100px; border-color: #4cae4c; background-image: linear-gradient(rgb(76,174,76) 0px, rgb(68,157,68) 100%);">添加</a>
    </div>
</body>
{{template "inc/easyui_js.html" .}}
<script type="text/javascript">
	function fmtStatus(val,row){
		if (val==1) {
			return "未启用";
		}else if (val==2){
			return "启用";
		}else{
			return "未知";
		}
	}
	function fmtCount(val,row){
		if (val>0) {
			return val;
		}else{
			return 0;
		}
	}
	function fmtMath(val,row){
		var count=0;
		if (row.adCount>0) {
			count=row.adCount;
		}
		var price=row.adCSPrice;
		return count*price;
	}
	function fmtAction(val,row){
		var res_str="";
		if (row.adStatus==1) {
			res_str+="&nbsp;<a href='javascript:;' onclick='ajAction(1,"+row.advertId+",0)'>启用</a>&nbsp;";
			{{if authas "/man/advert/toup" .PhyAuths}}
				res_str+="&nbsp;<a href='{{.baseUrl}}/man/advert/toup?id="+row.advertId+"'>修改</a>&nbsp;";
			{{end}}
		}else if(row.adStatus==2){
			res_str+="&nbsp;<a href='javascript:;' onclick='ajAction(2,"+row.advertId+",0)'>禁用</a>&nbsp;";
			res_str+="&nbsp;<a href=javascript:;' onclick='addClick("+row.advertId+")'>添加浏览量</a>&nbsp;";
		}
		return res_str;
	}
	//向后台进行操作 type 1启用 2禁用
	function ajAction(type,id,count){
		$.ajax({
			async:false,
			data:{id:id,type:type,count:count},
			dataType:'json',
			error:function (XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus);
			},
		  	type: "POST",
		  	url: "{{.baseUrl}}/man/advert/action",
		  	success:function(data){
		  		if (!data.result) {
		  			alert(data.msg);
		  		}else{
		  			$('#dg').datagrid('reload');
		  		}

		  		if (type==3) {
					$('#dlg').dialog('close');
		  		}
		  	}
		});
	}
	function addClick(id){
		$("#adCount").numberspinner({
			required:false
		});
		$("#ad_click_id").val(id);
		$("#adCount").numberspinner('setValue', 1);
		$('#dlg').dialog('open');
	}
	$(document).ready(function(){
		$("#btn_submit").click(function(){
			$("#adCount").numberspinner('enableValidation');

			var id=$("#ad_click_id").val();
			var count=$("#adCount").val();
			if (count>0) {
				ajAction(3,id,count);
			}
		});
	});
</script>
</html>